package org.theonept.travian.t4a.village;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.theonept.travian.model.Production;
import org.theonept.travian.model.Village;
import org.theonept.travian.t4a.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListVillageAdapter extends ArrayAdapter<Village> {
	private List<Village> data = new ArrayList<Village>();

	/**
	 * @return the data
	 */
	public List<Village> getData() {
		return data;
	}

	/**
	 * @param data
	 *            the data to set
	 */
	public void setData(List<Village> data) {
		this.data = data;
	}

	private final Activity context;

	public ListVillageAdapter(Activity context) {

		super(context, R.layout.village_list_view);
		this.context = context;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Village getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.valueOf(data.get(position).getIdVillage());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Village village = data.get(position);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.village_list_view, parent, false);
		TextView villageName = (TextView) rowView.findViewById(R.id.liste_village_name);
		TextView villageCoordonate = (TextView) rowView.findViewById(R.id.liste_village_coordornate);
		TextView villagePourcentDepot = (TextView) rowView.findViewById(R.id.liste_village_pourcentage_depot);
		TextView villagePourcentSilot = (TextView) rowView.findViewById(R.id.liste_village_pourcentage_silot);
		villageName.setText(village.getNomVillage());
		villageCoordonate.setText("(" + village.getPositionX() + " | " + village.getPositionY() + ")");
		Production production = village.getProduction(false);
		List<Integer> maxInt = new ArrayList<Integer>();
		maxInt.add(production.getStockBois());
		maxInt.add(production.getStockFer());
		maxInt.add(production.getStockArgile());
		Integer maxValue = Collections.max(maxInt);
		Integer pourcentDepot = (maxValue * 100) / production.getStockMaxRessources();
		Integer pourcentSilot = (production.getStockCereale() * 100) / production.getStockMaxCereale();
		villagePourcentDepot.setText(pourcentDepot + "%");
		villagePourcentSilot.setText(pourcentSilot + "%");
		if (village.getNbUnderAttack() > 0) {
			AnimationDrawable drawable = (AnimationDrawable) context.getResources().getDrawable(
					R.drawable.animation_att1);

			ImageView im = (ImageView) rowView.findViewById(R.id.liste_village_nb_under_attack_image);

			im.setBackgroundResource(R.drawable.animation_att1);
			AnimationDrawable rocketAnimation = (AnimationDrawable) im.getBackground();
			rocketAnimation.start();

			TextView tview = (TextView) rowView.findViewById(R.id.liste_village_nb_under_attack);
			tview.setVisibility(View.VISIBLE);
			tview.setText(village.getNbUnderAttack() + "");
			im.setVisibility(View.VISIBLE);
		}

		if (village.getNbAttack() > 0) {
			TextView tview = (TextView) rowView.findViewById(R.id.liste_village_nb_attack);
			tview.setVisibility(View.VISIBLE);
			tview.setText(village.getNbAttack() + "");
			rowView.findViewById(R.id.liste_village_nb_attack_image).setVisibility(View.VISIBLE);
		}

		if (village.getNbUnderAssistance() > 0) {
			TextView tview = (TextView) rowView.findViewById(R.id.liste_village_nb_under_assist);
			tview.setVisibility(View.VISIBLE);
			tview.setText(village.getNbUnderAssistance() + "");
			rowView.findViewById(R.id.liste_village_nb_under_assist_image).setVisibility(View.VISIBLE);
		}

		if (village.getNbAssistance() > 0) {
			TextView tview = (TextView) rowView.findViewById(R.id.liste_village_nb_assist);
			tview.setVisibility(View.VISIBLE);
			tview.setText(village.getNbUnderAssistance() + "");
			rowView.findViewById(R.id.liste_village_nb_assist_image).setVisibility(View.VISIBLE);
		}

		if (village.getHeroAdventure()) {
			TextView tview = (TextView) rowView.findViewById(R.id.liste_village_hero_adventure);
			tview.setVisibility(View.VISIBLE);
			tview.setText("1");
			rowView.findViewById(R.id.liste_village_hero_adventure_image).setVisibility(View.VISIBLE);
		}

		TextView loyauty = (TextView) rowView.findViewById(R.id.liste_village_loyauty);
		loyauty.setText(Html.fromHtml(village.getLoyaute()));

		return rowView;
	}

}
